package rt.crashmere.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import rt.crashmere.response.common.ErrorCode;
import rt.crashmere.response.exception.BusinessException;
import rt.crashmere.rpc.provider.InterfaceInfoProvider;
import rt.crashmere.server.mapper.InterfaceInfoMapper;
import rt.crashmere.server.model.domain.InterfaceInfo;
import rt.crashmere.server.service.InterfaceInfoService;

/**
 * @author Lwh
 * @description 针对表【interface_info(接口信息)】的数据库操作Service实现
 * @createDate 2023-01-24 16:18:58
 */
@Service
@DubboService
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoProvider, InterfaceInfoService {

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
    }

    @Override
    public int getInterfaceIdByPath(String path) {
        if (StringUtils.isBlank(path)) {
            return 0;
        }
        QueryWrapper<InterfaceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("url", path);
        InterfaceInfo interfaceInfo = this.getOne(wrapper);
        if (interfaceInfo == null) {
            return 0;
        }
        return interfaceInfo.getId();
    }
}




